package com.sam.widget.headerBar;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.sam.widget.R;


public abstract class BaseHeaderView extends LinearLayout{
    protected Context context;
    private LinearLayout headerLeft,headerCenter,headerRight;
    private boolean isShowLeft,isShowCenter,isShowRight;
    private int headerViewLeftPadding,headerViewRightPadding;

    public BaseHeaderView(Context context) {
        this(context,null);
    }

    public BaseHeaderView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public BaseHeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        getAttrs(attrs);
        findView();
        initView();
    }

    protected void getAttrs(AttributeSet attrs){
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.BaseHeaderView);
        isShowLeft = ta.getBoolean(R.styleable.BaseHeaderView_showLeft,true);
        isShowCenter = ta.getBoolean(R.styleable.BaseHeaderView_showCenter,true);
        isShowRight = ta.getBoolean(R.styleable.BaseHeaderView_showRight,true);
        headerViewLeftPadding = ta.getDimensionPixelSize(R.styleable.BaseHeaderView_headerViewLeftPadding,0);
        headerViewRightPadding = ta.getDimensionPixelSize(R.styleable.BaseHeaderView_headerViewRightPadding,0);
        ta.recycle();
    }

    protected void findView(){
        View headerView = View.inflate(context, R.layout.widget_header_view,this);
        headerLeft = (LinearLayout) headerView.findViewById(R.id.header_left);
        headerCenter = (LinearLayout) headerView.findViewById(R.id.header_center);
        headerRight = (LinearLayout) headerView.findViewById(R.id.header_right);
    }

    public abstract View getLeftView();
    public abstract View getCenterView();
    public abstract View getRightView();

    protected void initView(){
        headerLeft.setVisibility(isShowLeft ? VISIBLE : INVISIBLE);
        headerCenter.setVisibility(isShowCenter ? VISIBLE : INVISIBLE);
        headerRight.setVisibility(isShowRight ? VISIBLE : INVISIBLE);

        headerLeft.setPadding(headerViewLeftPadding,0,0,0);
        headerRight.setPadding(0,0,headerViewRightPadding,0);

        if(getLeftView() != null) {
            getLeftView().setClickable(false);
            getLeftView().setDuplicateParentStateEnabled(true);
            headerLeft.addView(getLeftView());
        }
        if(getCenterView() != null){
            getCenterView().setClickable(false);
            getCenterView().setDuplicateParentStateEnabled(true);
            headerCenter.addView(getCenterView());
        }
        if(getRightView() != null) {
            getRightView().setClickable(false);
            getRightView().setDuplicateParentStateEnabled(true);
            headerRight.addView(getRightView());
        }

        headerLeft.setOnClickListener(onClickListener);
        headerCenter.setOnClickListener(onClickListener);
        headerRight.setOnClickListener(onClickListener);
    }

    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int i = v.getId();
            if (i == R.id.header_left) {
                if (onHeaderClickListner != null) onHeaderClickListner.onHeaderLeftClicked();
                else if (context instanceof Activity) ((Activity) context).finish();

            } else if (i == R.id.header_center) {
                if (onHeaderClickListner != null) onHeaderClickListner.onHeaderCenterClicked();

            } else if (i == R.id.header_right) {
                if (onHeaderClickListner != null) onHeaderClickListner.onHeaderRightClicked();

            } else {
            }
        }
    };

    private OnHeaderClickListener onHeaderClickListner;
    public void setOnHeaderClickListner(OnHeaderClickListener onHeaderClickListner){
        this.onHeaderClickListner = onHeaderClickListner;
    }

    public interface OnHeaderClickListener {
        void onHeaderLeftClicked();
        void onHeaderCenterClicked();
        void onHeaderRightClicked();
    }

}
